﻿using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Web;
using System.Text;
using System.Configuration;
using System.Net.Mime;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Web.UI;
namespace BAL
{
    public class Common
    {
        public static SqlConnection con = new SqlConnection(BAL.conn.connectionString);
        public static void SendEmail(string From_Address, string To_Address, string Subject, string Content)
        {
            try
            {
                if (To_Address.Trim().Length == 0) { To_Address = ConfigurationManager.AppSettings["ToEmailID"].ToString(); }
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress(ConfigurationManager.AppSettings["FromEmailID"].ToString());

                mail.Subject = Subject;
                string content = Content;
                SmtpClient client = new SmtpClient();
                client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FromEmailID"].ToString(), ConfigurationManager.AppSettings["EmailPassword"].ToString());
                client.Port = 25;
                string p = HttpContext.Current.Request.UserHostAddress;
                client.Host = ConfigurationManager.AppSettings["EmailHost"].ToString();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                AlternateView av1 = AlternateView.CreateAlternateViewFromString(content, null, MediaTypeNames.Text.Html);

                LinkedResource logo = new LinkedResource(HttpContext.Current.Server.MapPath("~/Images/logo.jpg"));

                logo.ContentId = "companylogo";
                
                logo.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
                av1.LinkedResources.Add(logo);
                mail.AlternateViews.Add(av1);
                mail.IsBodyHtml = true;
                mail.To.Add(To_Address);
                client.Send(mail);
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }


        public static void SendEmailToAdmin(string From_Address, string To_Address, string Subject, string Content)
        {
            try
            {
                if (To_Address.Trim().Length == 0) { To_Address = ConfigurationManager.AppSettings["ToEmailID"].ToString(); }
                MailMessage mail = new MailMessage();
                //mail.From = new MailAddress("host2unlimited@yahoo.com");
                mail.From = new MailAddress(ConfigurationManager.AppSettings["FromEmailID"].ToString());
                mail.CC.Add(new MailAddress(ConfigurationManager.AppSettings["FromEmailID"].ToString()));
                mail.Subject = Subject;
                string content = Content;
                SmtpClient client = new SmtpClient();
                //client.Credentials = new System.Net.NetworkCredential("host2unlimited@yahoo.com", "123456789");
                client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FromEmailID"].ToString(), ConfigurationManager.AppSettings["EmailPassword"].ToString());
                client.Port = 25;
                string p = HttpContext.Current.Request.UserHostAddress;
                client.Host = ConfigurationManager.AppSettings["EmailHost"].ToString();//"smtp.mail.yahoo.com";//ConfigurationManager.AppSettings["EmailHost"].ToString();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                AlternateView av1 = AlternateView.CreateAlternateViewFromString(content, null, MediaTypeNames.Text.Html);

                LinkedResource logo = new LinkedResource(HttpContext.Current.Server.MapPath("~/Images/logo1 (1).jpg"));

                logo.ContentId = "companylogo";
                logo.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
                av1.LinkedResources.Add(logo);

                mail.AlternateViews.Add(av1);
                mail.IsBodyHtml = true;
                mail.To.Add(To_Address);
                client.Send(mail);
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
 

        //public static void logError(String sHeader, String sError)
        //{
        //    String sPath;
        //    String sActualError;
        //    StreamWriter errWriter;
        //    try
        //    {
        //        //' Get Actual Path of "Error" stored in Web.config
        //        sPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + ConfigurationManager.AppSettings["sPathErrorLog"];
        //        //sPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "ErrorLog\\";

        //        //' Generates Path & LogFile Name of  ErrorLog
        //        sPath = sPath + DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + ".log";

        //        //' Generates Error Message
        //        sActualError = System.DateTime.Now + " : " + sHeader + " : " + sError;

        //        //' Creation of File.
        //        errWriter = new StreamWriter(sPath, true, System.Text.Encoding.ASCII);

        //        errWriter.WriteLine("");
        //        errWriter.WriteLine(sActualError);
        //        errWriter.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {
        //    }
        //}

     
        public static string[] GetLoginDetails(int UserId,int login)
        {
            SqlParameter[] arrParam = new SqlParameter[2];

            arrParam[0] = new SqlParameter("@UserId", SqlDbType.Int);
            arrParam[0].Value = UserId;

            arrParam[1] = new SqlParameter("@login", SqlDbType.Int);
            arrParam[1].Value = login;

            SqlDataReader dr = CTEA_INDIA.DAL.SqlHelper.ExecuteReader(con, CommandType.StoredProcedure, "CT_GetLoginDetails", arrParam);

            int fields = dr.FieldCount;

            string[] Details = new string[fields];
            while (dr.Read())
            {
                for (int i = 0; i < fields; i++)
                {
                    Details[i] = dr[i].ToString();
                }
            }

            dr.Close();

            return Details;

        }

        public static void SendEmail(string From_Address, string To_Address, string Subject, string Content, int ccFlag)
        {
            try
            {

                if (To_Address.Trim().Length == 0) { To_Address = ConfigurationManager.AppSettings["ToEmailID"].ToString(); }
                MailMessage mail = new MailMessage();
                //mail.From = new MailAddress("host2unlimited@yahoo.com");
                mail.From = new MailAddress(ConfigurationManager.AppSettings["FromEmailID"].ToString());
                mail.Subject = Subject;
                string content = Content;

                if (ccFlag == 1)
                {
                    mail.CC.Add(ConfigurationManager.AppSettings["CC"].ToString());
                }
                SmtpClient client = new SmtpClient();
                //client.Credentials = new System.Net.NetworkCredential("host2unlimited@yahoo.com", "123456789");
                client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FromEmailID"].ToString(), ConfigurationManager.AppSettings["EmailPassword"].ToString());
                client.Port = 25;
                string p = HttpContext.Current.Request.UserHostAddress;
                client.Host = ConfigurationManager.AppSettings["EmailHost"].ToString();//"smtp.mail.yahoo.com";//ConfigurationManager.AppSettings["EmailHost"].ToString();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                AlternateView av1 = AlternateView.CreateAlternateViewFromString(content, null, MediaTypeNames.Text.Html);

                LinkedResource logo = new LinkedResource(HttpContext.Current.Server.MapPath("~/images/logo.jpg"));

                logo.ContentId = "companylogo";
                logo.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
                av1.LinkedResources.Add(logo);

                mail.AlternateViews.Add(av1);
                mail.IsBodyHtml = true;
                mail.To.Add(To_Address);
                client.Send(mail);
            }
            catch (Exception exp)
            {
                throw exp;
            }

        }
    }
}

